/**
 * 
 */
package lt.vin.currency.server.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import lt.vin.currency.server.PMF;
import lt.vin.currency.server.dao.CurrencyDao;
import lt.vin.currency.shared.Rate;
import lt.vin.currency.shared.Source;

import com.google.appengine.api.datastore.Text;

/**
 * @author Vincentas
 * 
 */
public class CurrencyDaoImpl implements CurrencyDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see lt.vin.currency.server.dao.CurrencyDao#get(java.lang.String,
	 * java.util.Date, java.lang.String)
	 */
	@Override
	public Rate get(String bankCode, Date date, String currency) {
		PersistenceManagerFactory pm = PMF.get();
		try {
			return null;
		} finally {
			pm.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see lt.vin.currency.server.dao.CurrencyDao#get(java.lang.String,
	 * java.util.Date)
	 */
	@Override
	public List<Rate> get(String bankCode, Date date) {
		PersistenceManagerFactory pm = PMF.get();
		try {
			return null;
		} finally {
			pm.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see lt.vin.currency.server.dao.CurrencyDao#get(java.lang.String)
	 */
	@Override
	public List<Rate> get(String bankCode) {
		PersistenceManagerFactory pm = PMF.get();
		try {
			return null;
		} finally {
			pm.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see lt.vin.currency.server.dao.CurrencyDao#get(java.util.Date)
	 */
	@Override
	public List<Rate> get(Date date) {
		PersistenceManagerFactory pm = PMF.get();
		try {
			return null;
		} finally {
			pm.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * lt.vin.currency.server.dao.CurrencyDao#save(lt.vin.currency.shared.Rate)
	 */
	@Override
	public void save(Source source) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(new SourceWrapper(source));
		} finally {
			pm.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see lt.vin.currency.server.dao.CurrencyDao#save(java.util.List)
	 */
	@Override
	public void save(List<Rate> rates) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistentAll(rates);
		} finally {
			pm.close();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see lt.vin.currency.server.dao.CurrencyDao#getSource(java.lang.String)
	 */
	@Override
	public Source getSource(String bankCode) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			return pm.getObjectById(SourceWrapper.class, bankCode).getSource();
		} finally {
			pm.close();
		}
	}
	

	@Override
	public List<Source> getSources() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Query query = pm.newQuery(SourceWrapper.class);
			try {				
				@SuppressWarnings("unchecked")
				List<SourceWrapper> wrappers = (List<SourceWrapper>) query.execute();
				List<Source> result = new ArrayList<Source>();
				
				for (SourceWrapper sourceWrapper : wrappers) {
					result.add(sourceWrapper.getSource());
				}
				
				return result;
			} finally {
				query.closeAll();
			}
		} finally {
			pm.close();
		}
	}

	@PersistenceCapable (detachable="true")
	private static class SourceWrapper {
		
		@PrimaryKey
		private String bankCode;

		@Persistent
		private String url;
		
		@Persistent
		private Text transformation;
		
		public SourceWrapper(Source source) {
			bankCode = source.getBankCode();
			url = source.getUrl();
			transformation = new Text(source.getTransformation());
		}
		
		public Source getSource() {
			return new Source(bankCode, url, transformation.getValue());
		}
	}
}
